package site.lanmushan.framework.datascope.autoconfiguration;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import site.lanmushan.framework.datascope.annotation.DataControl;
import site.lanmushan.framework.datascope.aspect.DataControlAspect;
import site.lanmushan.framework.datascope.datasource.DataControlMybatisInterceptor;
import site.lanmushan.framework.datascope.interceptor.DefaultDataControlSlashDataControl;
import site.lanmushan.framework.datascope.support.ISlashDataControl;
import site.lanmushan.framework.datascope.support.SlashDataControlInterceptor;

import javax.annotation.PostConstruct;
import java.util.Collection;

/**
 * @author Administrator
 */
@Configuration
@Import({DefaultDataControlSlashDataControl.class,
         DataControlMybatisInterceptor.class,
         DataControlAspect.class,
         DataScopeAutoConfiguration.class,
         AutoUpdateAutoConfiguration.class,
})

@Slf4j
public class DataControlAutoConfiguration {
    @Autowired(required = false)
    private Collection<SlashDataControlInterceptor> interceptors;
    @Autowired
    ISlashDataControl slashDataControl;
    @PostConstruct
    void init() {
        if (interceptors != null) {
            for (SlashDataControlInterceptor interceptor : interceptors) {
                DataControl dataControl = interceptor.getClass().getAnnotation(DataControl.class);
                if(dataControl==null)
                {
                    continue;
                }
                for (int i = 0; i < dataControl.value().length; i++) {
                    slashDataControl.registerInterceptor(dataControl.value()[i], interceptor);
                }
            }
        }
    }
}
